﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace QUT.Bio.SilverMap {

	/// <summary>
	/// A button-like object that represents a Blast HSP and provides a click method.
	/// </summary>

	public partial class HitButton : UserControl {
		private bool isDown = false;
		private bool highlight = false;

		#region Event: Click
		/// <summary>
		/// Provides button-like click event notifications.
		/// </summary>

		public event RoutedEventHandler Click;
		#endregion

		#region Constructor
		/// <summary>
		/// Constructor
		/// </summary>

		public HitButton () {
			InitializeComponent();
			
			MouseLeave += this.MouseLeaveEventListener;
			MouseLeftButtonDown += this.MouseLeftButtonDownEventListener;
			MouseLeftButtonUp += this.MouseLeftButtonUpEventListener;
			
			SetColor();
		}
		#endregion

		#region Method: MouseLeaveEventListener
		/// <summary>
		/// MouseLeave event listener: clears the siDown flag, so user will have to
		/// click again to instigate a Click.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>

		private void MouseLeaveEventListener ( object sender, MouseEventArgs e ) {
			isDown = false;
			SetColor();
		}
		#endregion

		private void SetColor () {
			border.Background = isDown 
			?	(Brush) ( highlight ? Resources["DepressedHighlightGradient"] : Resources["DepressedNormalGradient"] )
			:	(Brush) ( highlight ? Resources["HighlightGradient"] : Resources["NormalGradient"] );
		}

		#region Method: MouseLeftButtonDownEventListener
		/// <summary>
		/// MouseLeftButtonDown event listener: records the beginning of a "Click" by setting the
		/// isDown flag.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>

		private void MouseLeftButtonDownEventListener ( object sender, MouseButtonEventArgs e ) {
			isDown = true;
			SetColor();
		}
		#endregion

		#region Method: MouseLeftButtonUpEventListener
		/// <summary>
		/// MouseLeftButtonUp event listener: recognises the completion of a "Click"
		/// if isDown is set; fires the Click event if non-null.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>

		private void MouseLeftButtonUpEventListener ( object sender, MouseButtonEventArgs e ) {
			if ( isDown ) {
				isDown = false;
				SetColor();

				if ( Click != null ) {
					Click( this, new RoutedEventArgs() );
				}
			}
		}
		#endregion

		#region Method: Highlight
		/// <summary>
		/// Changes the color to show the button is highlighted.
		/// </summary>

		public void Highlight () {
			highlight = true;
			SetColor();
		}
		#endregion

		#region Method: Normal
		/// <summary>
		/// Changes the color to show the button is not highlighted.
		/// </summary>

		public void Normal () {
			highlight = false;
			SetColor();
		}
		#endregion

	}
}
